package com.lzh.runke.core.autoconfigure;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.boot.autoconfigure.AutoConfigurationExcludeFilter;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.List;

//TODO ? 自动注册mapper
@Configuration
@EnableConfigurationProperties(CoreConfigProperties.class)
@Import(CoreAutoConfiguration.AutoScannerRegistry.class)
@ConditionalOnBean(SqlSessionFactory.class)
@AutoConfigureAfter(MybatisAutoConfiguration.class)
@MapperScan("com.lzh.runke.mapper")
@EnableScheduling
public class CoreAutoConfiguration implements WebMvcConfigurer, InitializingBean {

    @Resource
    private List<SqlSessionFactory> sqlSessionFactoryList;

    @Resource
    private CoreConfigProperties coreConfigProperties;

    @Override
    public void afterPropertiesSet() {
        setupMybatis();
    }

    private void setupMybatis() {
        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
            org.apache.ibatis.session.Configuration configuration = sqlSessionFactory.getConfiguration();

            configuration.setCacheEnabled(false);
            configuration.setCallSettersOnNulls(true);

            configuration.getTypeAliasRegistry().registerAliases("com.rkinf.framework.core.entity");
        }
    }

    @Slf4j
    static class AutoScannerRegistry implements BeanDefinitionRegistryPostProcessor {

        @Override
        public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
            ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry);
            scanner.addExcludeFilter(new AutoConfigurationExcludeFilter());
            int numOfBeans = scanner.scan("com.rkinf.framework.core");
            log.info("Auto scanner registered {} beans", numOfBeans);
        }

        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

        }
    }
}
